<?php

class application extends applicationEngine {

    public $defaultPage = 'home';
    protected $authenticated = false;

    public function init(){

        # Add some pages
        $this->addPage('home','pageHome();');
        $this->addPage('homeLogin','pageLoginSucces();');
        $this->addPage('uitloggen','pageUitloggen();');

        # Add a form
        $this->addForm('loginform');
        $this->addForm('registerform');
        $this->addForm('searchform');
        $this->addForm('aanmeldenritform');

        parent::init();

    }

    public function createMainPage($title = '', $template = 'defaultpage', $text = '', $toonZoeken = true) {

        $page = parent::createMainPage();

        $page->addCssFile('style', false);
        $page->assign($title, 'title');

        # Add the maintemplate
        $page->appendTemplate($template,'mainContent');

        # Menu toevoegen
        $mainMenu = $this->createMainMenu();
        $page->assign($mainMenu,'mainMenu');

        # Get the mainmenu
        $mainMenu = $this->createMainMenu();
        $page->assign($mainMenu,'mainmenu');

        return $page;

    }

    protected function pageError($errorMsg) {

        $page = $this->createMainPage();

        return $page;

    }

    protected function createMainMenu() {

        # Setup the pages we have
        $menuItems = array(
            array(
                'page' => 'home',
                'value' => 'Home',
                'type' => 'page'
            ),

        );

        // Alleen wanneer je niet bent ingelogd de onderstaande items tonen in menu
        if($_SESSION['authenticated'] == false){

            $menuItems[] = array(
                'application' => 'authentication',
                'page' => 'loginiframe',
                'value' => 'Inloggen',
                'type' => 'popup',
                'width' => '500',
                'height' => '300'
            );

            $menuItems[] = array(
                'page' => 'register',
                'application' => 'authentication',
                'value' => 'Registreren',
                'type' => 'page'
            );

        } else {

						$menuItems[] = array(
                'page' => 'rittenoverzicht',
                'value' => 'Mijn ritten',
                'application' => 'ritten',
                'type' => 'page'
            );

						$menuItems[] = array(
                'page' => 'aanmeldenrit',
                'value' => 'Aanmelden rit',
                'application' => 'ritten',
                'type' => 'page'
            );

            $menuItems[] = array(
                'page' => 'uitloggen',
                'value' => 'Uitloggen',
                'type' => 'page'
            );

        }

        $menuHtml = '<ul>';

        # Concat al menuitems
        foreach($menuItems as $menuitem) {

            $url = (isset($menuitem['application']) ? '?application='.$menuitem['application'] : '');
            $url .= ($url == '' ? '?' : '&') . (isset($menuitem['page']) ? 'page='.$menuitem['page'] : '');
            $url .= ($url == '' ? '?' : '&') . (isset($menuitem['height']) || isset($menuitem['width']) ? 'width='.$menuitem['width'].'&height='.$menuitem['height'] : '');
            $anchorOptions = ($menuitem['type'] == 'popup' ? 'class="openModal"' : '');

            $menuHtml .= '<li><a href="index.php'.$url.'" '.$anchorOptions.'>'.$menuitem['value'].'</a></li>';

        }

        $menuHtml .= '</ul>';

        return $menuHtml;

    }

    public function pageHome() {

        $page = $this->createMainPage('ITract.nl', 'homepage');

        $this->loadForm('searchform',$page,'searchform');

        return $page;

    }

    public function pageLoginSucces() {

        $page = $this->pageHome();

        return $page;

    }

    public function pageUitloggen() {

        // We zijn succesvol uitgelogd
        $_SESSION['authenticated'] = false;
        $_SESSION['klantid'] = false;

        $page = $this->pageHome();

        return $page;

    }

}

?>
